<template>
  <el-dialog title="修改地质平面图纸" :visible.sync="dialogEditorVisible" width="40%" :before-close="handleClose">
    <el-form ref="form" :model="currentRow" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="名称">
            <el-input v-model="currentRow.title"></el-input>
          </el-form-item></el-col>
        <el-col :span="12">
          <el-form-item label="标段">
            <el-select v-model="currentRow.titleNo" placeholder="请选择标段">
              <el-option value="第三" label="第三" />
              <el-option value="第四" label="第四" />
              <el-option value="第五" label="第五" />
            </el-select>
          </el-form-item></el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input type="textarea" v-model="currentRow.changeInfo"></el-input>
          </el-form-item></el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="文件">
            <el-upload name="file" class="upload-demo" action="string" :http-request="beforeAvatarUploadThree"
            :on-preview="handlePreview"
             :on-remove="handleRemove" 
             :before-remove="beforeRemove" multiple :limit="3"
            :on-success="handlesuccess" 
            :on-error="handleerror" 
            :on-exceed="handleExceed" 
            :on-progress="uploadVideoProcess"
            :file-list="fileList">
            <el-button size="small" type="primary">更新图纸</el-button>
          </el-upload>
          <el-progress v-if="progressFlag" :percentage="loadProgress"></el-progress>
          </el-form-item>
          </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogEditorVisible = false">取 消</el-button>
      <el-button type="primary" @click="create">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import api from '../../../../api'
import qs from 'qs'

export default {
  data() {
    return {
      dialogEditorVisible: false,
      tunnelList: [],
      fileList: [],
      currentRow: {},
      loadProgress: 0, // 动态显示进度条
      progressFlag: false, // 关闭进度条
    }
  },
  props: {
    flag: {
      type: Boolean,
      default: false
    }
  },
  mounted() {
    this.$bus.$on('onShowEditor', (row) => {
      this.dialogEditorVisible = true
      this.currentRow = row
    })
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => { })
    },
    create() {
      api
        .AddrPlaneFileupdate(qs.stringify(this.currentRow))
        .then((res) => {
          if (res.data.code == 2001) {
            this.$message({
              type: 'success',
              message: '更新成功!'
            })
            this.dialogEditorVisible = false
            this.$bus.$emit('onRefreshAddrPlane', true)
          }
        })
        .catch((err) => { })
    },
    onSubmit() {
      console.log('submit!')
    },
    handlesuccess(response, file, fileList) {
      console.log(response)
      if (response.code == 2001) {
        this.currentRow.resources = response.data
      }
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`
      )
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleerror(err, file, fileList) {
      console.log(err)
    },
    beforeAvatarUploadThree(file) {
      let params = new FormData()
      params.append('file', file.file)
      api
        .AddrPlaneFileUpload(params)
        .then((res) => {
          if (res.data.code == 2001) {
            this.currentRow.resources = res.data.data
          }
        })
        .catch((err) => { })
    },
    uploadVideoProcess(event, file, fileList){
      this.progressFlag = true; // 显示进度条
          this.loadProgress = parseInt(event.percent); // 动态获取文件上传进度
          if (this.loadProgress >= 100) {
              this.loadProgress = 100
              setTimeout( () => {this.progressFlag = false}, 1000) // 一秒后关闭进度条
          }
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  width: 50px !important;
  height: 50px !important;
  font-size: 28px;
  line-height: 50px !important;
  color: #8c939d;
  text-align: center;
}

.avatar {
  display: block;
  width: 50px !important;
  height: 50px !important;
}

.imgbeforepad {
  padding-top: 0px !important;
}

.avatar-uploader .el-icon-plus {
  margin: 15px !important;
}
</style>